package cn.zq.zqclock;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import java.text.SimpleDateFormat;
import java.util.Locale;

/**
 * ================================================
 * 作    者：程志强
 * 邮    箱：zhiqiang8838@163.com
 * 版    本：1.0.0
 * 创建日期：2022/08/15  11:15
 * 包    名：cn.zq.zqclock
 * 描    述：
 * 修订历史：
 * ================================================
 */
public class ClockView extends View {

    private final Paint mPaint;

    private final SimpleDateFormat sdf;

    //表盘半径
    private float radius;

    private int w;

    private int h;

    private final int timeColor;

    private final float textSize;

    public ClockView(Context context) {
        this(context, null);
    }

    public ClockView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ClockView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        sdf = new SimpleDateFormat("HH:mm:ss", Locale.CHINESE);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ClockView);
        timeColor = ta.getColor(R.styleable.ClockView_timeColor, 0xFF000000);
        textSize = ta.getFloat(R.styleable.ClockView_textSize, 55);
        float clockStrokeWidth = ta.getFloat(R.styleable.ClockView_clockStrokeWidth, 5);
        ta.recycle();

        mPaint = new Paint();
        mPaint.setStrokeWidth(clockStrokeWidth);
        mPaint.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        w = width;
        h = height;

        radius = Math.min(width, height) / 2.0f - mPaint.getStrokeWidth() / 2.0f;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        this.w = w;
        this.h = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mPaint.setColor(0xFFFFFFFF);

        //画圆
        mPaint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(getWh(), getHh(), radius, mPaint);

        //画刻度
        if (w < h) {
            for (int i = 0; i < 60; i++) {
                if (i % 5 == 0) {
                    canvas.drawLine(getWh(), getHh() - getWh(), getWh(), getHh() - radius / 10 * 8, mPaint);
                    canvas.rotate(6.0f, getWh(), getHh());
                    continue;
                }
                canvas.drawLine(getWh(), getHh() - getWh(), getWh(), getHh() - radius / 10 * 9, mPaint);

                canvas.rotate(6.0f, getWh(), getHh());
            }
        } else {
            for (int i = 0; i < 60; i++) {
                if (i % 5 == 0) {
                    canvas.drawLine(getWh(), 0, getWh(), getHh() - radius / 10 * 8, mPaint);
                    canvas.rotate(6.0f, getWh(), getHh());
                    continue;
                }
                canvas.drawLine(getWh(), 0, getWh(), getHh()- radius / 10 * 9, mPaint);

                canvas.rotate(6.0f, getWh(), getHh());
            }
        }

        //画数字
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setTextSize(textSize);
        for (int i = 0; i < 12; i++) {
            canvas.save();
            String num = i + 1 + "";
            canvas.rotate(30.0f * (i + 1), getWh(), getHh());
            canvas.rotate(360 - 30 * (i + 1), getWh(), getHh() - radius / 10 * 7 - (-(mPaint.ascent() + mPaint.descent()) / 2));

            canvas.drawText(num, getWh() - mPaint.measureText(num) / 2.0f, getHh() - radius / 10 * 7, mPaint);
            canvas.restore();
        }

        //画中心点
        canvas.drawCircle(getWh(), getHh(), 20, mPaint);

        long currentTimeMillis = System.currentTimeMillis();

        String format = sdf.format(currentTimeMillis);
        String[] split = format.split(":");

        int hour = Integer.parseInt(split[0]);
        int minute = Integer.parseInt(split[1]);
        int second = Integer.parseInt(split[2]);

        mPaint.setColor(timeColor);
        canvas.drawText(format, getWh() - mPaint.measureText(format) / 2, getHh() + radius / 3, mPaint);

        mPaint.setColor(0xFFFFFFFF);
        //时针
        canvas.save();
        canvas.rotate(hour * 30 + minute / 2.0f, getWh(), getHh());
        canvas.drawLine(getWh(), getHh() - radius / 10 * 4, getWh(), getHh() + radius / 10, mPaint);

        canvas.restore();
        canvas.save();
        //分针
        canvas.rotate(6.0f * minute, getWh(), getHh());
        canvas.drawLine(getWh(), getHh() - radius / 10 * 7, getWh(), getHh() + radius / 10, mPaint);

        canvas.restore();
        //秒针
        canvas.rotate(6.0f * second, getWh(), getHh());
        canvas.drawLine(getWh(), getHh() - radius / 10 * 8, getWh(), getHh() + radius / 10 * 2, mPaint);

        postInvalidateDelayed(1000);
    }

    /**
     * 获取宽度
     */
    private int getWh() {
        return getWidth() / 2;
    }

    /**
     * 获取高度
     */
    private int getHh() {
        return getHeight() / 2;
    }
}
